package com.ruoyi.radar.controller;

import com.ruoyi.coal.common.utils.R;
import com.ruoyi.coal.modules.coalpile.controller.CoalYardInfoController;
import com.ruoyi.coal.modules.coalpile.domain.CoalYardInfoEntity;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.radar.domain.RadarSilo;
import com.ruoyi.radar.mapper.RadarSiloMapper;
import com.ruoyi.radar.service.RadarSiloService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.*;

@CrossOrigin
@RestController
@RequestMapping("/radarSilo")
public class RadarSiloController extends BaseController {

    @Autowired
    RadarSiloService radarSiloService;

    @Autowired
    RadarSiloMapper radarSiloMapper;

    @Autowired
    CoalYardInfoController coalYardInfoController;
    /**
     * 获取料仓列表
     */
//    @PreAuthorize("@ss.hasPermi('radarSilo:silo:list')")
    @GetMapping("/list")
//    @Transactional
    public AjaxResult list(@RequestParam Map<String, Object> param)
//    public TableDataInfo list(GnssEquip equip)
    {
//        startPage();
        Map<String, Object> result = new HashMap<>(2);
//        Map<String, Object> gnssMap = radarSiloService.selectSiloList(param);
        List<RadarSilo> radarSilos = radarSiloService.selectSiloList(param);
        Integer count = radarSiloService.getCount(param);
        R list = coalYardInfoController.list(param);
        result.put("siloData", radarSilos);
        Object page = list.get("page");

        result.put("coalData",page);
        result.put("total", count);
        return AjaxResult.success(result);
    }

    @PostMapping("/siloWithConfList")
    public AjaxResult siloWithConfList(@RequestBody Map<String, Object> param)
    {

        List<RadarSilo> radarSilos = radarSiloService.selectSiloWithConfList(param);

        return AjaxResult.success(radarSilos);
    }


    /**
     * 新增料仓
     */

//    public class CoalYardInfoController {
//        @Autowired
//        private CoalYardInfoService coalYardInfoService;
    @PreAuthorize("@ss.hasPermi('radarSilo:silo:add')")
    @Log(title = "料仓管理", businessType = BusinessType.INSERT)
    @PostMapping
//    @Transactional
    public AjaxResult add(@RequestBody RadarSilo radarSilo)
    {
        try{
            radarSilo.setCreateTime(currentTime());
            radarSilo.setUpdateTime(currentTime());
            radarSilo.setViewHeight(radarSilo.getSiloHeight());
            boolean save = radarSiloService.save(radarSilo);
            CoalYardInfoEntity coalYardInfoEntity = new CoalYardInfoEntity();
            coalYardInfoEntity.setId(radarSilo.getSiloStockId());
            coalYardInfoEntity.setName(radarSilo.getSiloStockName());
            coalYardInfoEntity.setDel(1);
            coalYardInfoEntity.setStatus(0);
            coalYardInfoEntity.setType(radarSilo.getType());
            coalYardInfoEntity.setJsonFilter(radarSilo.getJsonFilter());
            coalYardInfoEntity.setBaseData(radarSilo.getBaseData());
            coalYardInfoEntity.setDensity(Double.parseDouble((radarSilo.getSiloDensity()==null)?"1":radarSilo.getSiloDensity()));
            coalYardInfoEntity.setAngle(radarSilo.getAngle());
            coalYardInfoEntity.setBottomDensity(radarSilo.getBottomDensity());
            coalYardInfoEntity.setBottomVolume(radarSilo.getBottomVolume());
            coalYardInfoEntity.setCreateTime(radarSilo.getCreateTime());
            coalYardInfoController.save(coalYardInfoEntity);
            return toAjax(save);
        }catch (Exception e){
            return AjaxResult.success("操作失败");
        }
    }

    /**
     *修改料仓
     */
    @PreAuthorize("@ss.hasPermi('radarSilo:silo:update')")
    @Log(title = "修改料仓", businessType = BusinessType.UPDATE)
    @PutMapping
//    @Transactional
    public AjaxResult edit(@Validated @RequestBody RadarSilo radarSilo)
    {
        try{
            radarSilo.setUpdateTime(currentTime());
            CoalYardInfoEntity coalYardInfoEntity = new CoalYardInfoEntity();
            coalYardInfoEntity.setId(radarSilo.getSiloStockId());
            coalYardInfoEntity.setName(radarSilo.getSiloStockName());
            coalYardInfoEntity.setDel(1);
            coalYardInfoEntity.setStatus(0);
            coalYardInfoEntity.setType(radarSilo.getType());
            coalYardInfoEntity.setJsonFilter(radarSilo.getJsonFilter());
            coalYardInfoEntity.setBaseData(radarSilo.getBaseData());
            coalYardInfoEntity.setDensity(Double.parseDouble((radarSilo.getSiloDensity()==null)?"1":radarSilo.getSiloDensity()));
            coalYardInfoEntity.setAngle(radarSilo.getAngle());
            coalYardInfoEntity.setBottomDensity(radarSilo.getBottomDensity());
            coalYardInfoEntity.setBottomVolume(radarSilo.getBottomVolume());
            coalYardInfoEntity.setCreateTime(radarSilo.getCreateTime());
            coalYardInfoController.update(coalYardInfoEntity);
            return toAjax(radarSiloService.updateById(radarSilo));
        }catch (Exception e){
            return AjaxResult.success("操作失败");
        }
    }

    /**
     * 删除料仓
     */
    @PreAuthorize("@ss.hasPermi('radarSilo:silo:remove')")
    @Log(title = "删除料仓", businessType = BusinessType.DELETE)
    @DeleteMapping("/{stockIds}")
    public AjaxResult remove(@PathVariable String[] stockIds)
    {
        ArrayList<String> ids = new ArrayList<>();
        Collections.addAll(ids, stockIds);
        Integer[] integers = Arrays.stream(stockIds).mapToInt(Integer::valueOf).boxed().toArray(Integer[]::new);
//        for(int i = 0;i <ids.size();i++){
//
//            integers[i] = Integer.valueOf(ids.get(i));
//        }
//        Integer[] integers = ids.toArray(new String[stockIds.length]);
        coalYardInfoController.delete(integers);
        return toAjax(radarSiloService.removeByIds(ids));
    }

    @Log(title = "料仓数据导出", businessType = BusinessType.EXPORT)
    @PreAuthorize("@ss.hasPermi('radarSilo:silo:export')")
    @PostMapping("/export")
    public void export(HttpServletResponse response, Map<String, Object> param)
    {
        List<RadarSilo> list = radarSiloService.selectSiloWithConfList(param);
//        List<GnssEquip> list = gnssEquipService.list(equip);
//        gnssEquipService
        ExcelUtil<RadarSilo> util = new ExcelUtil<RadarSilo>(RadarSilo.class);
        util.exportExcel(response, list, "设备数据");
    }
    //
    @Log(title = "料仓数据导入", businessType = BusinessType.IMPORT)
    @PreAuthorize("@ss.hasPermi('radarSilo:silo:import')")
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file, boolean updateSupport) throws Exception
    {
        ExcelUtil<RadarSilo> util = new ExcelUtil<RadarSilo>(RadarSilo.class);
        List<RadarSilo> radarSilos = util.importExcel(file.getInputStream());
        String operName = getUsername();
        String message = radarSiloService.importSilo(radarSilos, updateSupport, operName);
        return success(message);
    }
    //
    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response)
    {
        ExcelUtil<RadarSilo> util = new ExcelUtil<RadarSilo>(RadarSilo.class);
        util.importTemplateExcel(response, "料仓数据");
    }


}
